/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-08-23 17:07:16
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-25 11:40:22
 */
import React, { useState, useEffect, useRef } from "react";
import { Form, Button, Input, Affix, Select, message, Space, Switch, Drawer, Row, Col, Modal } from "antd";
import { MinusCircleOutlined, PlusCircleOutlined } from '@ant-design/icons';
import { useModelAssociateType } from "@/components";
import cmdbres from "@/data/cmdbres";
import hcpiam from "@/data/hcpiam";
import request, {requestIAM} from '@/utils/request';
import regexpUtil from "@/utils/regexpUtil";
import hcpUtils from "@/utils/hcpUtils";

const user = hcpUtils.getUser();
const SimpModule = ({ rowKeys, visible, setVisible, onFinish, cmModuleId}) => {
    const [form] = Form.useForm();
    const [bizData, setBizData] = useState([]);
    const [dataLoading, setDataLoading] = useState(false);
    const [loading, setLoading] = useState(false);

    const queryBizList = async () => {
        try {
            setDataLoading(true);
            const res = await request({
                ...cmdbres.queryBizList,
                data: {
                    pageNumber:1,
                    pageSize:1000,
                    sort:{
                        bizCode:"ASC",
                        createTime:"DESC"
                      }
                }
            });
            setDataLoading(false);
            if (res && res.code == 200) {
                let arr = res.data?.filter(item=> item.cm_unid !=  "0")??[];
                setBizData(arr);
            }
        } catch (error) {
            setBizData([]);
            console.log(error);
        }
    }

    useEffect(() => {
        queryBizList();
    }, []);

    const onSubmit = async (values) => {
        setLoading(true);
		let res = await request({
			...cmdbres.assignService(),
			data: {
                bizId: values.biz_id,
                serverIds: rowKeys,
                author: user.staffName,
                cmModuleId,
            }
		});
		setLoading(false);
		if (res && res.code == 200) {
			setVisible(false);
			message.success(`分配到业务空闲机池成功！`);
			if (typeof onFinish == "function") onFinish(res);
		}
	};

    const onCancel = () => {
        setVisible(false);
    }

    return (
        <Modal
            className="hcp-tip-modal hcp-modal-sm"
            title="分配到业务空闲机池"
            visible={visible}
            onCancel={onCancel}
            footer={null}
            destroyOnClose
            maskClosable={false}
            afterClose={() => {
                form.resetFields();
            }}
        >
            <Form
                form={form}
                labelCol={{ span: 5}}
                wrapperCol={{ span: 17 }}
                name="basic"
                onFinish={onSubmit}
            >
                <p>&nbsp;&nbsp;已选择【{rowKeys.length}】台主机</p>
                <Form.Item
                    label="业务列表"
                    name="biz_id"
                    rules={[{required: true,message: '请选择'}]}
                    style={{marginBottom: 0}}
                >
                    <Select
                        placeholder="请选择"
                        style={{ width: "100%", marginRight: 10 }}
                        showSearch
                        loading={dataLoading}
                        filterOption={(input, option) =>
                            option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                        }
                        getPopupContainer={(triggerNode) => triggerNode.parentElement}
                    >
                        {
                            bizData?.map((item, index) => (
                                <Select.Option
                                    key={item.cm_unid}
                                    value={item.cm_unid}
                                >
                                    {item.biz_name}
                                </Select.Option>
                            ))
                        }
                    </Select>
                </Form.Item>
                <div className="hcp-modal-body-footer" style={{marginTop: 20}}>
                    <Form.Item wrapperCol={{ span: 24 }}>
                        <Button onClick={onCancel} >
                            取消
                        </Button>
                        <Button type="primary" htmlType="submit" loading={loading}>
                            提交
                        </Button>
                    </Form.Item>
                </div>
            </Form>
        </Modal>
    );
};

export default SimpModule;